import { getDetail } from '@/apis/detail'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate } from 'vue-router'

export function useDetail() {
  const goods = ref({})
  const route = useRoute()
  const getGoods = async (id = route.params.id) => {
    const res = await getDetail(id)
    goods.value = res.result
  }
  onMounted(() => getGoods())

  // 目标:路由参数变化的时候 可以把分类数据接口重新发送
  onBeforeRouteUpdate(to => {
    // console.log('路由变化了')
    // console.log(to)
    getGoods(to.params.id)
  })

  return {
    goods
  }
}
